// ToolView.cpp : implementation file
//

#include "stdafx.h"
#include "TestAnaly.h"
#include "ToolView.h"

#include "MainFrm.h"
// CToolView

IMPLEMENT_DYNCREATE(CToolView, CFormView)

CToolView::CToolView()
: CFormView(CToolView::IDD)
{
	m_pMainFrame = NULL;
}

CToolView::~CToolView()
{
}

void CToolView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SLIDER_binaryimg, m_SliderBinaryImg);
}

BEGIN_MESSAGE_MAP(CToolView, CFormView)
	ON_BN_CLICKED(IDC_BUTTON_open, &CToolView::OnBnClickedButtonopen)
	ON_BN_CLICKED(IDC_BUTTON_edge, &CToolView::OnBnClickedButtonedge)
	ON_BN_CLICKED(IDC_BUTTON_findarea, &CToolView::OnBnClickedButtonfindarea)
	ON_BN_CLICKED(IDC_BUTTON_grade, &CToolView::OnBnClickedButtongrade)
	ON_BN_CLICKED(IDC_BUTTON_BinaryImg, &CToolView::OnBnClickedButtonBinaryimg)
	ON_NOTIFY(TRBN_THUMBPOSCHANGING, IDC_SLIDER_binaryimg, &CToolView::OnTRBNThumbPosChangingSliderbinaryimg)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BUTTON_open_checkfile, &CToolView::OnBnClickedButtonopencheckfile)
END_MESSAGE_MAP()


// CToolView diagnostics

#ifdef _DEBUG
void CToolView::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CToolView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG



void CToolView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO: Add your specialized code here and/or call the base class
	
}

void CToolView::Initialize()
{
	m_SliderBinaryImg.SetRange(0,255);

	m_ImagePro.Initialize();

	m_ImgAnaly.Initialize();
	m_ImgAnaly.SetHwnd( m_pMainFrame->m_pStandardView->m_hWnd );
}


// CToolView message handlers

void CToolView::OnBnClickedButtonopen()
{
	// TODO: Add your control notification handler code here
	char szModulePath[MAX_PATH] = "";
	char szFileName[MAX_PATH] = "";
  
// 	GetModuleFileName(NULL,szModulePath,MAX_PATH);
// 	char* sTemp = strrchr(szModulePath,'\\'); 
// 	sTemp++;
// 	sTemp[0] = '\0'; 
// 
// 	sprintf(szFileName,"%stest.tiff",szModulePath);



	TCHAR *pstrFilter = _T("bmp(*.bmp)|*.bmp");

	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,pstrFilter);
	if( IDOK != dlg.DoModal() )
	{
		return ;
	}

	sprintf( szFileName,"%s",dlg.GetPathName() );

	m_ImagePro.LoadFile(szFileName);

	zkImgBitInfo *pImgInfo = NULL;
	m_ImagePro.GetImgBuffer(&pImgInfo);

	m_ImgAnaly.SetStdImgBuffer(pImgInfo);


	int cx,cy;
	m_ImagePro.GetImageSize(cx,cy);

	CSize sScroll;
	sScroll.cx = cx;
	sScroll.cy = cy;
	m_pMainFrame->m_pStandardView->SetScrollSizes( MM_TEXT,sScroll );

	m_pMainFrame->m_pStandardView->DDOnDraw();
}
 
void CToolView::OnBnClickedButtonedge()
{
	// TODO: Add your control notification handler code here
	m_ImgAnaly.StdImgOperate(ImgAnaly_EdgeSharp);

	m_pMainFrame->m_pStandardView->DDOnDraw();
}

void CToolView::OnBnClickedButtongrade()
{
	// TODO: Add your control notification handler code here
	m_ImgAnaly.StdImgOperate(ImgAnaly_GradsSharp);

	m_pMainFrame->m_pStandardView->DDOnDraw();
}

void CToolView::OnBnClickedButtonfindarea()
{
	// TODO: Add your control notification handler code here
	m_ImgAnaly.StdImgOperate(ImgAnaly_FindArea);

	m_pMainFrame->m_pStandardView->DDOnDraw();
}






void CToolView::OnTRBNThumbPosChangingSliderbinaryimg(NMHDR *pNMHDR, LRESULT *pResult)
{
	// This feature requires Windows Vista or greater.
	// The symbol _WIN32_WINNT must be >= 0x0600.
	NMTRBTHUMBPOSCHANGING *pNMTPC = reinterpret_cast<NMTRBTHUMBPOSCHANGING *>(pNMHDR);
	// TODO: Add your control notification handler code here
	*pResult = 0;
}

void CToolView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	if( pScrollBar == GetDlgItem(IDC_SLIDER_binaryimg) )
	{
        if( nPos > 0 )
		{
			m_SliderBinaryImg.SetPos(nPos);

			int nNewPos = nPos * 256;

			UINT nBinaryImg[4] = {0};
			nBinaryImg[0] = nNewPos;
			m_ImgAnaly.ImageCtrl(ImgAnaly_BinaryImage,nBinaryImg);
			m_pMainFrame->m_pStandardView->DDOnDraw();

			SetDlgItemInt(IDC_EDIT_binaryimage,nNewPos);
		}	
	}

	CFormView::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CToolView::OnBnClickedButtonBinaryimg()
{
	UINT nBinaryImg[4] = {0};
	nBinaryImg[0] = GetDlgItemInt(IDC_EDIT_binaryimage);

	m_ImgAnaly.StdImgOperate(ImgAnaly_BinaryImage,nBinaryImg);

	m_pMainFrame->m_pStandardView->DDOnDraw();
}

void CToolView::OnBnClickedButtonopencheckfile()
{
	char szFileName[MAX_PATH] = "";

	TCHAR *pstrFilter = _T("bmp(*.bmp)|*.bmp");

	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,pstrFilter);
	if( IDOK != dlg.DoModal() )
	{
		return ;
	}

	sprintf( szFileName,"%s",dlg.GetPathName() );

	m_ImagePro.LoadFile(szFileName);

	zkImgBitInfo *pImgInfo = NULL;
	m_ImagePro.GetImgBuffer(&pImgInfo);

	m_ImgAnaly.SetChkImgBuffer(pImgInfo);


	int cx,cy;
	m_ImagePro.GetImageSize(cx,cy);

	CSize sScroll;
	sScroll.cx = cx;
	sScroll.cy = cy;
	m_pMainFrame->m_pStandardView->SetScrollSizes( MM_TEXT,sScroll );

	m_pMainFrame->m_pStandardView->DDOnDraw();
}
